<?php

$downloadPath = 'http://www.superjetinternational.com/wp-content/themes/carrington-text/resumes/';

if (!empty($_POST))
{
	
    if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
  
 		$error = "none";
		//Check if the file is JPEG image and it's size is less than 350Kb
		$filename = basename($_FILES['uploaded_file']['name']);
		$ext = substr($filename, strrpos($filename, '.') + 1);
		if (($ext == "zip" || $ext == "pdf") && ($_FILES["uploaded_file"]["type"] == "application/zip" || $_FILES["uploaded_file"]["type"] == "application/pdf") && ($_FILES["uploaded_file"]["size"] < 750000)) 			{
			// random 4 digit to add to our file name
			// some people use date and time in stead of random digit
			$random_digit=rand(0000,9999);
			
			//combine random digit to you file name to create new file name
			//use dot (.) to combile these two variables
			
			//Determine the path to which we want to save this file
		  	//$newname = dirname(__FILE__).'/resumes/'.$random_digit.$filename;
		  	
		  	$newname = dirname(__FILE__).'/resumes/'.$_POST['name'].'_'.$_POST['surname'].'_cv_'.$random_digit.'.'.$ext;
		  	
		  	$downloadPath = $downloadPath.$_POST['name'].'_'.$_POST['surname'].'_cv_'.$random_digit.'.'.$ext;
		  	
		 	//Check if the file with the same name is already exists on the server
		  	if (!file_exists($newname)) {
		    	//Attempt to move the uploaded file to it's new place
		    	if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
		    	
		    		sendMail($newname, $downloadPath);
		    		//echo "file uploaded:".$newname;
		    	} else {
		       		$error ="Error: A problem occurred during file upload!";
		    	}
		  	} else {
		  		$error = "Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
		  	}
		} else {
		 	$error = "Error: Only .jpg images under 350Kb are accepted for upload";
		 }
	}else{
		$error = "uploadError";
	}
	
	if($error != "none")
	{
		//echo $error;
		header("Location: /careers/work-with-us/upload-error/");
	}
} 

function sendMail($filename, $download){
	// multiple recipients
	//$to  = 'hr@superjetinternational.com' . ', '; // note the comma
//	$to .= 'enrico@vangogh-creative.it';


	$to  = 'hr@superjetinternational.com';
	// subject
	$subject = 'Resume from SuperjetInternational.com';
	
	// message
	$message = 'A resume has beed received by: '.$_POST['name'].' '.$_POST['surname'].'. The resume is available to download from: '.$download.'. NOTES: '.$_POST['notes'];
	
	// Additional headers
	
	$headers = "Reply-To: ".$_POST['name']." ".$_POST['surname']." <".$_POST['email'].">\r\n"; 
    $headers .= "Return-Path: Superjet International <info@superjetinternational.com>\r\n"; 
    $headers .= "From: Superjet International <info@superjetinternational.com>\r\n"; 
    $headers .= "Organization: Superjet International\r\n"; 
    $headers .= "Content-Type: text/plain\r\n";
	
	// Mail it
	mail($to, $subject, $message, $headers);
	
	header("Location: /careers/work-with-us/message-confirmation/");
}
 
?>